package com.scoy.honeymei.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.EvaAdapter;
import com.scoy.honeymei.bean.GoodsOrderBean;
import com.scoy.honeymei.bean.ShopTicketBean;
import com.scoy.honeymei.bean.StarShowBean;
import com.scoy.honeymei.databinding.DialogTicketCheckBinding;
import com.scoy.honeymei.databinding.DialogToEvaBinding;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:      com.oylib.dialog
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2019/12/31/0031$ 下午 15:57$
 * UpdateRemark:   更新内容
 */
public class EvaDialog extends DialogFragment {


    private DialogToEvaBinding binding;
    private Context mContext;
    private int shopId;
    private EvaAdapter hAdapter;
    private String shopName;
    private ArrayList<GoodsOrderBean> orderBeanList;
    private int orderId;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            orderId = getArguments().getInt("orderId", 0);
            shopId = getArguments().getInt("shopId", 0);
            shopName = getArguments().getString("shopName");
            orderBeanList = getArguments().getParcelableArrayList("orderBeanList");
        }
    }

    public static EvaDialog newInstance(int orderId, int shopId, String shopName, ArrayList<GoodsOrderBean> orderBeanList) {

        Bundle args = new Bundle();
        args.putInt("orderId", orderId);
        args.putInt("shopId", shopId);
        args.putString("shopName", shopName);
        args.putParcelableArrayList("orderBeanList", orderBeanList);
        EvaDialog fragment = new EvaDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = DialogToEvaBinding.inflate(inflater, container, false);
        mContext = getContext();

        initView();

        return binding.getRoot();
    }

    private void initView() {

        initRv();

        binding.evaShopnameTv.setText(shopName);

        binding.dtcSureTv.setOnClickListener(v -> {
            if (binding.ratingShopBar.getNumStars() == 0) {
                MyUtil.mytoast(mContext, "请对商家进行评分");
                return;
            }
            List<GoodsOrderBean> data = hAdapter.getData();

            //  ArrayList<StarShowBean> starList = new ArrayList<>();
            ArrayList<String> starList = new ArrayList<>();

            for (GoodsOrderBean mBean : data) {
                int starTemp = mBean.getStarTemp();
                starList.add(mBean.getId() + "|" + mBean.getStarTemp());
                if (starTemp == 0) {
                    MyUtil.mytoast(mContext, "请对所有商品进行评分");
                    return;
                }
            }
            httpDataList(MyUtil.arrToString(starList));

        });

        binding.dtcCloseIv.setOnClickListener(v -> dismiss());
        binding.dtcCancelTv.setOnClickListener(v -> dismiss());
    }

    @Override
    public void onStart() {
        super.onStart();
        Window dialogWindow = getDialog().getWindow();
        if (dialogWindow != null) {
            dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
            dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialogWindow.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;
            lp.windowAnimations = android.R.style.Animation_InputMethod;
            dialogWindow.setAttributes(lp);
        }
    }

    private void initRv() {
        hAdapter = new EvaAdapter(mContext);
        binding.dtcRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        DividerItemDecoration did = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        did.setDrawable(getResources().getDrawable(R.drawable.divider_gray_line_pd));
        binding.dtcRv.addItemDecoration(did);
        binding.dtcRv.setAdapter(hAdapter);

        hAdapter.setNewData(orderBeanList);
    }

    public interface OnDialogListener {
        void onSuccess();
    }

    OnDialogListener onDialogListener;

    public EvaDialog setOnDialogListener(OnDialogListener onDialogListener) {
        this.onDialogListener = onDialogListener;
        return this;
    }


    private void httpDataList(String toJs) {
        HttpParams params = new HttpParams();
        params.put("order_id", orderId);
        params.put("shop_star", shopId);
        params.put("goods_star", toJs);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.SHOP_EVALUATE, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                onDialogListener.onSuccess();
                MyUtil.mytoast(mContext, "提交成功");
                dismiss();
            }

        });

    }

}
